# This example shows how to subscribe to events and display the event message with each notification, using a regular
# callback method.
#
# Find all latest examples here: https://opclabs.doc-that.com/files/onlinedocs/OPCLabs-OpcStudio/Latest/examples.html .
# OPC client and subscriber examples in Python on GitHub: https://github.com/OPCLabs/Examples-QuickOPC-Python .
# Missing some example? Ask us for it on our Online Forums, https://www.opclabs.com/forum/index ! You do not have to own
# a commercial license in order to use Online Forums, and we reply to every post.
# The QuickOPC package is needed. Install it using "pip install opclabs_quickopc".
import opclabs_quickopc
import time
# Import .NET namespaces.
from OpcLabs.EasyOpc import *
from OpcLabs.EasyOpc.AlarmsAndEvents import *
# Notification callback.
def callback(sender, e):
assert e is not None
if not e.Succeeded:
print('*** Failure: ', e.ErrorMessageBrief, sep='')
return
else:
if e.EventData is not None:
print(e.EventData.Message)
# Instantiate the client object
client = EasyAEClient()
print('Subscribing events...')
# The callback is a regular method that displays the event message.
handle = IEasyAEClientExtension.SubscribeEvents(client,
'', 'OPCLabs.KitEventServer.2', 1000,
EasyAENotificationEventHandler(callback))
print('Processing event notifications for 20 seconds...')
time.sleep(20)
print('Unsubscribing events...')
client.UnsubscribeAllEvents()
print('Waiting for 2 seconds...')
time.sleep(2)
print('Finished.')